#!/bin/bash
#完成开机进程监测
#IP=10.75.108.134
#IP=10.75.98.194
IP=192.168.1.253
connect_adb(){
	adb connect $IP
	while [ $?  == 1 ]; do
		echo "adb not connected,ready to connect"
		adb connect $IP
	done
	adb root
	adb connect $IP
	adb root
	while [ $?  == 1 ]; do
		echo "adb not root,ready to root"
		adb connect $IP
		adb root
	done
	adb connect $IP
}

reboot_tv(){
	echo 'exec reboot cmd'
	adb shell reboot reboot &
	sleep 60s
        adb kill-server
	sleep 2s
	adb connect $IP
	sleep 2s
	adb root
	adb connect $IP
	connect_adb
	echo 'finished connect adb'
}
get_procrank(){
	rm -rf reboot_result
	if [ ! -d "reboot_result/" ]; then
		mkdir reboot_result
	fi
	cd reboot_result
	mkdir procrank
	cd ..


	count=1
	while [ $count -le 50 ];do
		if [ -f stop ];then
		    echo "Found stop file!!!"
		    break
		fi
	
		time=`date "+%H:%M:%S"`
		focusedAc=`adb shell dumpsys activity | grep 'mFocusedActivity' | awk '{print $4}'`
		echo -en "$time\t$focusedAc\n" > reboot_result/procrank/$count.txt
		adb shell procrank | grep -v 'warning' >> reboot_result/procrank/$count.txt
		sleep 10
		count=$(( $count + 1))

	done
}

get_procrank_inTV(){
	rm -rf result
	if [ ! -d "result/" ]; then
		mkdir result
	fi
	
	adb push getprocrank.sh /data/local/tmp
	while [ $?  == 1 ]; do
		echo "adb not root,ready to root"
		adb connect $IP
		adb root
		adb push getprocrank.sh /data/local/tmp
	done
	adb connect $IP
	adb push stop.sh /data/local/tmp
	adb shell sh /data/local/tmp/getprocrank.sh &
	adb shell sh /data/local/tmp/stop.sh &
}

get_meminfo_inTV(){

	rm -rf result
	if [ ! -d "result/" ]; then
		mkdir result
	fi
	
	adb push getmeminfo.sh /data/local/tmp
	while [ $?  == 1 ]; do
		echo "adb not root,ready to root"
		adb connect $IP
		adb root
		adb push getmeminfo.sh /data/local/tmp
	done
	adb connect $IP
	adb push stop.sh /data/local/tmp
	adb shell sh /data/local/tmp/getmeminfo.sh &
	adb shell sh /data/local/tmp/stop.sh &

}

connect_adb
echo 'start to reboot TV'
date "+%H:%M:%S"
reboot_tv
echo 'finished reboot TV'
date "+%H:%M:%S"
echo 'start to exec get_meminfo_inTV cmd'
get_meminfo_inTV
echo 'finished exec get_meminfo_inTV cmd '
sleep 2m
echo 'finished wait 10m'
echo 'start to pull result'
date "+%H:%M:%S"
adb pull /data/local/tmp/result  result/
echo 'start to parsemem result'
date "+%H:%M:%S"
python parseMeminfo.py
echo 'finished parsemem result'
date "+%H:%M:%S"


